#include "queue.h"

/*
 * List
 *
 * list all the PID(s) in a given queue
 */
void list(struct process_struct* list, int size)
{
    int i = 0;

    if(size == 0)
    {
        printf("Empty List\n");
    }
   
    for(i; i<size; i++)
    {
        printf("%d: %d\n", i, list->PID);

        list = list->next_process; 
    }
}

/*
 * Enqueue
 *
 * add a process to the queue
 */
struct process_struct * 
enqueue(struct process_struct **head, 
        struct process_struct  *item, 
        int                    *size)
{
    if((*size) == 0)
    {
        (*head) = item;

        (*head)->next_process = (*head); 
        (*head)->prev_process = (*head); 

    } else {
        item->next_process = (*head);
        item->prev_process = (*head)->prev_process;

        (*head)->prev_process->next_process = item;
        (*head)->prev_process               = item;


        (*head) = item;
    }

    (*size)++;

    return (*head); 
}

/*
 * Dequeue
 *
 * dequeue a process from the queue. 
 */
struct process_struct * 
dequeue(struct process_struct **head, 
        int                    *size)
{
    struct process_struct * value;

    if((*size) == 0){
        return NULL;

    } else if((*size) == 1){
        value = (*head);
        (*head) = NULL;    

    } else {
        value = (*head)->prev_process;

        value->prev_process->next_process = (*head);
        (*head)->prev_process             = value->prev_process;

    }

    (*size)--;

    return value;
}

/*
 * Search through the queue and find the node that matches 
 * the search 
 */
struct process_struct * 
find_node(struct process_struct **head, 
          int                    *size, 
          int                     search)
{
    struct process_struct * item; 
    int i = 0;
    int found = 0;
    
    item = (*head);
    for(i; i<(*size); i++)
    {
       if((item->PID) == search)
       {
            found = 1;
            break;
       }

       item = item->next_process;

    }

    if(found == 0)
    {
        return NULL;

    } else {
        return item;

    }
}

/*
 * Remove Node
 *
 * remove a item off the queue based on the PID
 */
struct process_struct * 
remove_node(struct process_struct **head, 
            int                    *size, 
            int                     search)
{

    struct process_struct * item; 
    int i = 0;
    int found = 0;
    
    item = (*head);
    for(i; i<(*size); i++)
    {
       if((item->PID) == search)
       {
            found = 1;
            break;
       }

       item = item->next_process;
    }

    if(found == 0)
    {
        return NULL;

    } else {
        if((*size) == 0){
            return NULL;
            
        } else if((*size) == 1){
            (*head) = NULL;    

        } else {
            if((*head) == item)
            {
                (*head) = (*head)->next_process;
            }

            item->prev_process->next_process = item->next_process;
            item->next_process->prev_process = item->prev_process;

        }

        (*size)--;

        return item;
    }
}
